package repository;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import domain.Academy;

public class AcademicDAO extends BaseDAO {

	private static final long serialVersionUID = 1247124013562600413L;

	public AcademicDAO() throws SQLException {
		super();
	}

	@Override
	public ResultSet query(Object... params) {
		String sql = "SELECT * FROM 学院表 WHERE 学院ID=?;";
		ResultSet rs = null;
		try {
			PreparedStatement pst = con.prepareStatement(sql);
			pst.setString(1, (String)params[0]);
			rs = pst.executeQuery();
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
	
	public ResultSet queryAll() {
		String sql = "SELECT * FROM 学院表;";
		ResultSet rs = null;
		
		try {
			PreparedStatement pst = con.prepareStatement(sql);			
			rs = pst.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}

	@Override
	public int update(Object... params) {
		String sql = "UPDATE 学院表 SET 联系方式=? WHERE 学院名称=?;";
		int num = 0;
		try {
			PreparedStatement pst = con.prepareStatement(sql);
			Academy acad = (Academy)params[0];
			pst.setString(1, acad.getConInf());
			pst.setString(1, acad.getName());
			num = pst.executeUpdate();
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return num;
	}

	@Override
	public int delete(Object... params) {
		String sql = "DELETE FROM 学院表 WHERE 学院ID=?;";
		int num = 0;
		try {
			PreparedStatement pst = con.prepareStatement(sql);
			pst.setString(1, (String)params[0]);
			num = pst.executeUpdate();
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return num;
	}

	@Override
	public int insert(Object... params) {
		String sql = "INSERT INTO 学院表(学院ID,学院名称,联系方式)"+"VALUES(?,?,?);";
		int num = 0;
		try {
			PreparedStatement pst = con.prepareStatement(sql);
			Academy acad = (Academy)params[0];
			pst.setString(1, acad.getID());
			pst.setString(2, acad.getName());
			pst.setString(3, acad.getConInf());
			num = pst.executeUpdate();
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return num;
	}

}
